import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont


def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)


def zh_ch(string):
    return string.encode("gbk").decode(errors="ignore")


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

    import cv2

    ##选择摄像头
    # 机位1
    capture1 = cv2.VideoCapture(0)



    while (True):



        (ret1, frame1) = capture1.read()
        #原始屏幕对比
        # cv2.imshow('video original', frame1)

        #仿射变换
        grayFrame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
        # (thresh1, blackAndWhiteFrame1) = cv2.threshold(grayFrame1, 111, 255, cv2.THRESH_BINARY)

        #边缘羽化卷积核尺寸
        blur = cv2.blur(grayFrame1, (45, 45))
        #cv2.imshow('video bw', blackAndWhiteFrame1)
        # cv2.putText(frame1,str, (word_x, word_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
        #cv2.imshow('video bw', blur)
        blur2 = cv2ImgAddText(blur, "演示版\n按esc退出", 10, 65, (0, 0, 139), 20)


        #全屏
        # load the image
        #img = cv2.imread('yue2.jpg')
        # define the screen resulation
        screen_res = 1280, 720
        scale_width = screen_res[0] / blur2.shape[1]
        scale_height = screen_res[1] / blur2.shape[0]
        scale = min(scale_width, scale_height)
        # resized window width and height
        window_width = int(blur2.shape[1] * scale)
        window_height = int(blur2.shape[0] * scale)
        # cv2.WINDOW_NORMAL makes the output window resizealbe
        cv2.namedWindow('Resized Window', cv2.WINDOW_NORMAL)
        # resize the window according to the screen resolution
        #cv2.resizeWindow('Resized Window', window_width, window_height)
        cv2.imshow('Resized Window', blur2)


        #cv2.imshow('video blur', blur2)






#按下 esc 键盘最左上角按键 退出程序
        if cv2.waitKey(1) == 27:
            break

    capture1.release()
    cv2.destroyAllWindows()


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('OPENCV')
